/**
 * 
 */
package jmine.tec.report.metadata;

import java.io.Serializable;

/**
 * Definição de parametro nao modificavel. Usado para descrever os parametros de uma coluna.
 * 
 * @author takeshi
 * @version 10/08/2007
 */
public class ParameterDefinition implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -1318598499417378495L;

    private final String name, label;

    private final Object reference;

    private final ParameterType type;

    /**
     * C'tor
     * 
     * @param name String
     * @param type the type
     */
    public ParameterDefinition(final String name, final ParameterType type) {
        this(name, name, null, type);
    }

    /**
     * C'tor
     * 
     * @param name the name
     * @param type the type
     * @param reference the reference
     */
    public ParameterDefinition(final String name, final ParameterType type, final Object reference) {
        this(name, name, reference, type);
    }

    /**
     * C'tor
     * 
     * @param name String the name of this parameter
     * @param label String the label of this parameter
     * @param ref the reference object (default value or list of entities)
     * @param type {@link ParameterType} the type of this parameter
     */
    public ParameterDefinition(final String name, final String label, final Object ref, final ParameterType type) {
        super();
        this.name = name;
        this.label = label;
        this.type = type;
        this.reference = ref;
    }

    /**
     * Compare equality
     * 
     * @param obj {@link Object}
     * @return boolean
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        final ParameterDefinition other = (ParameterDefinition) obj;
        if (this.label == null) {
            if (other.label != null) {
                return false;
            }
        } else if (!this.label.equals(other.label)) {
            return false;
        }
        if (this.name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!this.name.equals(other.name)) {
            return false;
        }
        if (this.type != other.type) {
            return false;
        }
        return true;
    }

    /**
     * @return the label
     */
    public String getLabel() {
        return this.label;
    }

    /**
     * Recupera o nome da coluna a ser usado como uma chave unica
     * 
     * @return the name
     */
    public String getName() {
        return this.name;
    }

    /**
     * Devolve um objeto de referencia, por exemplo um Map para um parametro do tipo {@link ParameterType#ENTITY}
     * 
     * @return the reference
     */
    public Object getReference() {
        return this.reference;
    }

    /**
     * Devolve o tipo deste parametro
     * 
     * @return the type
     */
    public ParameterType getType() {
        return this.type;
    }

    /**
     * returns hashcode
     * 
     * @return int
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.label == null) ? 0 : this.label.hashCode());
        result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
        result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
        return result;
    }

    /**
     * Returns wheter or not the given value is compatible with the given parameter
     * 
     * @param value {@link Object}
     * @return boolean
     */
    public boolean isCompatible(final Object value) {
        return this.type.allowedType(value.getClass());
    }

    /**
     * @return {@link String}
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return this.name + " (" + this.label + ") -> " + this.type;
    }

}
